package com.openspring.framework.jdbc.sql;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class Insert {

	private String tableName;
	private Map<String, String> columns = new LinkedHashMap<String, String>();
	
	public Insert setTableName(String tableName) {
		this.tableName = tableName;
		return this;
	}
	
	public Insert addColumn(String columnName) {
		return addColumn(columnName, "?");
	}

	public Insert addColumn(String columnName, String valueExpression) {
		columns.put(columnName, valueExpression);
		return this;
	}

	public String toInsert() {
		StringBuilder sb = new StringBuilder();
		sb.append("INSERT INTO ").append(tableName).append(" (");
		Iterator<String> iter = columns.keySet().iterator();
		while (iter.hasNext()) {
			sb.append(iter.next());
			if (iter.hasNext()) {
				sb.append(", ");
			}
		}
		sb.append(") VALUES (");
		iter = columns.values().iterator();
		while (iter.hasNext()) {
			sb.append(iter.next());
			if (iter.hasNext()) {
				sb.append(", ");
			}
		}
		sb.append(')');
		return sb.toString();
	}
}
